package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pojo.Book;
import pojo.Topic;
import pojo.User;

import java.util.*;

import dao.BookDAO;
import dao.CommentDAO;
import dao.TopicDAO;
import dao.UserDAO;
 
@Controller
public class TimKiemNangCaoController {
	private static String iName="";
	private static String iContent="";
	private static int iTopic=-1;
	private static String iAuthor="";
	private static String iPub="";	
	private static String iTran="";
	private static String iloainxb="";
	private static int iThang=-1;
	private static int iNam=-1;
	private static int btt=-1;
	private static int page=-1;
	private static int kieuSort=1;
	private static int giathap=-1;
	private static int giacao=-1;
	
    @RequestMapping(value= "/TimKiemNangCao.do" , method = RequestMethod.GET)
    public String getview(
			@RequestParam(value="tensach", required = false) String _tensach,
			@RequestParam(value="content", required = false) String _noidung,
			@RequestParam(value="topic", required = false) String _topic,
			@RequestParam(value="tacgia", required = false) String _tacgia,
			@RequestParam(value="dichgia", required = false) String _dichgia,
			@RequestParam(value="nhaxuatban", required = false) String _nxb,
			@RequestParam(value="loaiNxb", required = false) String _loainxb,
			@RequestParam(value="thang", required = false) String _thang,
			@RequestParam(value="nam", required = false) String _nam,
			@RequestParam(value="kieuSort", required = false) String _kieusort,
    		@RequestParam(value ="trang", required = false) String trang,
			@RequestParam(value ="slbooktt", required = false) String _btt,
			@RequestParam(value ="BatDau", required = false) String batdau,
			@RequestParam(value ="giathap", required = false) String _giathap,
			@RequestParam(value ="giacao", required = false) String _giacao,
			@RequestParam(value ="save", required = false) String save,
			Model model,
			HttpServletRequest request
    		) {
    	if(batdau!= null)
    	{
	    	TopicDAO td= new TopicDAO();
	    	List<Topic> lmenu1=td.getMenuCap1();
	    	model.addAttribute("lmenu1",lmenu1);
	    	return ("page/FrontEnd/TimKiemNangCao");
    	}
    	if(save.equals("false"))
    	{
	   		 iName="";
	   		 iContent="";
	   		 iTopic=-1;
	   		 iAuthor="";
	   		 iPub="";	
	   		 iTran="";
	   		 iloainxb="";
	   		 iThang=-1;
	   		iNam=-1;
	   		 btt=-1;
	   		page=-1;
	   		kieuSort=1;
   		
    	}
    	if(_tensach!=null &&save.equals("false") ) iName=_tensach;
    	if(_noidung!=null  &&save.equals("false")) iContent=_noidung;
    	if(_topic!=null) iTopic=Integer.parseInt(_topic);
    	if(_tacgia!=null ) iAuthor=_tacgia;
    	if(_dichgia!=null ) iTran=_dichgia;
    	if(_nxb!=null ) iPub=_nxb;
    	if(_loainxb!=null) iloainxb=_loainxb;
    	if(_thang!=null) iThang=Integer.parseInt(_thang);
    	if(_nam !=null )
    		{
    			if(!_nam.equals(""))
    			iNam=Integer.parseInt(_nam);
    		}
    	if(_kieusort!=null) kieuSort=Integer.parseInt(_kieusort);
    	if(trang!=null) page=Integer.parseInt(trang);
    	if(_btt!=null) btt=Integer.parseInt(_btt);
    	if(_giathap!=null) giathap=Integer.parseInt(_giathap);
    	if(_giacao!=null) giacao=Integer.parseInt(_giacao);
 
    	//String oldUrl=st2+"?headerinput="+input+"&trang="+Integer.toString(page)+"&slbooktt="+Integer.toString(btt)+"&kieuSort="+Integer.toString(kieuSort)+"&save="+save+"&headertopic="+Integer.toString(id);
    	//session.setAttribute("oldUrl",oldUrl);
    	HttpSession session = request.getSession();
    	String st2= request.getRequestURL().toString();
    	String oldUrl=st2+"?headerinput="+iName+"&trang="+Integer.toString(page)+"&slbooktt="+Integer.toString(btt)+"&kieuSort="+Integer.toString(kieuSort)+"&save=true"+"&headertopic="+Integer.toString(iTopic);
    	session.setAttribute("oldUrl",oldUrl);
    	TopicDAO td= new TopicDAO();
    	
    	Topic tp= td.getTopicByID(iTopic);
    	BookDAO bd= new BookDAO();
    	long tongsotrang=bd.tinhSoTrangTimKiemNangCao(iName, iContent, tp, iAuthor, iTran, iPub, iloainxb, iNam, iThang, btt,giathap,giacao);
    	List<Book> lb=bd.getBookTimKiemNangCao(iName, iContent, tp, iAuthor, iTran, iPub, iloainxb, iNam, iThang, btt, page, kieuSort,giathap,giacao);
    	long tongketqua =bd.tinhKetQuaTimKiemNangCao(iName, iContent, tp, iAuthor, iTran, iPub, iloainxb, iNam, iThang, btt,giathap,giacao);
    	
    	String kq= "Có "+Long.toString(tongketqua) +" kết quả";
    	model.addAttribute("ketqua", kq);
    	model.addAttribute("lb",lb);
    	model.addAttribute("lb",lb);
    	model.addAttribute("Servlet","TimKiemNangCao.do?save=true");
    	model.addAttribute("tongSoTrang",tongsotrang);
    	model.addAttribute("trangHienTai",page);
    	model.addAttribute("slbooktt",btt);
    	model.addAttribute("kieuSort",kieuSort);
    	model.addAttribute("title1","Kết quả tìm kiếm nâng cao");
    	model.addAttribute("title2",null);
    	model.addAttribute("tukhoa",null);
    	return ("page/FrontEnd/KetQuaTimKiem");
    }
   
    
    @RequestMapping(value= "/TimKiemNangCao.do" , method = RequestMethod.POST)
    public String getview2(
			@RequestParam(value="tensach", required = false) String _tensach,
			@RequestParam(value="content", required = false) String _noidung,
			@RequestParam(value="topic", required = false) String _topic,
			@RequestParam(value="tacgia", required = false) String _tacgia,
			@RequestParam(value="dichgia", required = false) String _dichgia,
			@RequestParam(value="nhaxuatban", required = false) String _nxb,
			@RequestParam(value="loaiNxb", required = false) String _loainxb,
			@RequestParam(value="thang", required = false) String _thang,
			@RequestParam(value="nam", required = false) String _nam,
			@RequestParam(value="kieuSort", required = false) String _kieusort,
    		@RequestParam(value ="trang", required = false) String trang,
			@RequestParam(value ="slbooktt", required = false) String _btt,
			@RequestParam(value ="save", required = false) String save,
			@RequestParam(value ="giathap", required = false) String _giathap,
			@RequestParam(value ="giacao", required = false) String _giacao,
			Model model,
			HttpServletRequest request
    		) {
    	
    	if(save.equals("false"))
    	{
	   		 iName="";
	   		 iContent="";
	   		 iTopic=-1;
	   		 iAuthor="";
	   		 iPub="";	
	   		 iTran="";
	   		 iloainxb="";
	   		 iThang=-1;
	   		 iNam=-1;
	   		 btt=-1;
	   		page=-1;
	   		kieuSort=1;
	   		giathap=-1;
	   		giacao=-1;
   		
    	}
    	if(_tensach!=null &&save.equals("false")  ) iName=_tensach;
    	if(_noidung!=null &&save.equals("false")) iContent=_noidung;
    	if(_topic!=null) iTopic=Integer.parseInt(_topic);
    	if(_tacgia!=null ) iAuthor=_tacgia;
    	if(_dichgia!=null ) iTran=_dichgia;
    	if(_nxb!=null ) iPub=_nxb;
    	if(_loainxb!=null) iloainxb=_loainxb;
    	if(_thang!=null) iThang=Integer.parseInt(_thang);
    	if(_nam !=null )
    		{
    			if(!_nam.equals(""))
    			iNam=Integer.parseInt(_nam);
    		}
    	if(_giathap!=null)
    	{
    		if(!_giathap.equals(""))
    		giathap=Integer.parseInt(_giathap);
    	}
    	if(_giacao!=null) {
    		if(_giacao!="")
    		giacao=Integer.parseInt(_giacao);
    	}
    	if(_kieusort!=null) kieuSort=Integer.parseInt(_kieusort);
    	if(trang!=null) page=Integer.parseInt(trang);
    	if(_btt!=null) btt=Integer.parseInt(_btt);
    	HttpSession session = request.getSession();
    	String st2= request.getRequestURL().toString();
    	String oldUrl=st2+"?headerinput="+iName+"&trang="+Integer.toString(page)+"&slbooktt="+Integer.toString(btt)+"&kieuSort="+Integer.toString(kieuSort)+"&save=true"+"&headertopic="+Integer.toString(iTopic);
    	session.setAttribute("oldUrl",oldUrl);
    	TopicDAO td= new TopicDAO();
    	
    	Topic tp= td.getTopicByID(iTopic);
    	BookDAO bd= new BookDAO();
    	long tongsotrang=bd.tinhSoTrangTimKiemNangCao(iName, iContent, tp, iAuthor, iTran, iPub, iloainxb, iNam, iThang, btt,giathap,giacao);
    	List<Book> lb=bd.getBookTimKiemNangCao(iName, iContent, tp, iAuthor, iTran, iPub, iloainxb, iNam, iThang, btt, page, kieuSort,giathap,giacao);
    	long tongketqua =bd.tinhKetQuaTimKiemNangCao(iName, iContent, tp, iAuthor, iTran, iPub, iloainxb, iNam, iThang, btt,giathap,giacao);
    	
    	String kq= "Có "+Long.toString(tongketqua) +" kết quả";
    	model.addAttribute("ketqua", kq);
    	model.addAttribute("lb",lb);
    	model.addAttribute("Servlet","TimKiemNangCao.do?save=true");
    	model.addAttribute("tongSoTrang",tongsotrang);
    	model.addAttribute("trangHienTai",page);
    	model.addAttribute("slbooktt",btt);
    	model.addAttribute("kieuSort",kieuSort);
    	model.addAttribute("title1","Kết quả tìm kiếm nâng cao");
    	model.addAttribute("title2",null);
    	model.addAttribute("tukhoa",null);
    	return ("page/FrontEnd/KetQuaTimKiem");
    }
}